from rest_framework import serializers

from account.serializer import AccountSerializer
from .models import Diy, Ware, Like, Collect, Browse

class DiySerializer(serializers.ModelSerializer):
    account = AccountSerializer(read_only=True)
    class Meta:
        model = Diy
        fields = [
            'id', 
            'account',
            'uuid', 
            'title', 
            'desc', 
            'price',
            'share',
            'status', 
            'update_time',
            'create_time'
        ]
        read_only_fields = ('id', 'account', 'uuid', 'status', 'share', 'update_time', 'create_time')


class WareSerializer(serializers.ModelSerializer):
    class Meta:
        model = Ware
        fields = [
            'title', 
            'desc', 
            'price',
            'count', 
            'create_time'
        ]
        read_only_fields = ('create_time', )


class DiyWareSerializer(serializers.ModelSerializer):
    account = AccountSerializer(read_only=True)
    wares = WareSerializer(many=True)
    class Meta:
        model = Diy
        fields = [
            'id', 
            'account',
            'uuid', 
            'title', 
            'desc', 
            'price',
            'share',
            'status', 
            'weapp_qrcode',
            'update_time',
            'create_time',
            'wares'
        ]
        read_only_fields = ('id', 'account', 'status', 'weapp_qrcode', 'update_time', 'create_time')
        # not required fields
        extra_kwargs = {
            'uuid': {'required': False},
        }

    def create(self, validated_data):
        wares_data = validated_data.pop('wares')
        diy = Diy.objects.create(**validated_data)
        # 统计内容为空的配件数量
        empty_count = 0
        for ware_data in wares_data:
            if 'desc' not in ware_data or len(ware_data['desc']) <= 0:
                empty_count += 1
            Ware.objects.create(diy=diy, **ware_data)
        # 如果为空的配件数量大于1则设置diy的分享字段为false
        if empty_count > 1:
            diy.share = False
            diy.save()
        return diy


class LikeSerializer(serializers.ModelSerializer):
    account = AccountSerializer(read_only=True)
    class Meta:
        model = Like
        fields = [
            'id', 
            'account',
            'diy',
            'update_time',
            'create_time'
        ]


class LikeDiySerializer(serializers.ModelSerializer):
    account = AccountSerializer(read_only=True)
    diy = DiyWareSerializer(read_only=True)
    class Meta:
        model = Like
        fields = [
            'id', 
            'account',
            'diy',
            'update_time',
            'create_time'
        ]


class CollectSerializer(serializers.ModelSerializer):
    account = AccountSerializer(read_only=True)
    class Meta:
        model = Collect
        fields = [
            'id', 
            'account',
            'diy',
            'update_time',
            'create_time'
        ]


class CollectDiySerializer(serializers.ModelSerializer):
    account = AccountSerializer(read_only=True)
    diy = DiyWareSerializer(read_only=True)
    class Meta:
        model = Collect
        fields = [
            'id', 
            'account',
            'diy',
            'update_time',
            'create_time'
        ]


class BrowseSerializer(serializers.ModelSerializer):
    class Meta:
        model = Browse
        fields = [
            'id', 
            'account',
            'diy',
            'update_time',
            'create_time'
        ]
